<?php
/**
 * DDButton creates fancy Vista like form buttons
 *
 * @author  Joachim Werner <joachim.werner@diggin-data.de>
 * @version 0.2
 */
class DDButton extends CWidget
{
    // {{{ Members
    public $linkTitle   = 'Submit this form';
    public $title       = 'Submit';
    public $buttonType  = 'submit';
    public $formID;
    public $url;
    public $confirm     = null;
    public $isFirst     = false;
    // }}} 
    // {{{ run
    public function run()
    {
        $this->registerClientScripts();
        echo $this->createMarkup();
    } // }}} 
    // {{{ createMarkup
    public function createMarkup()
    {
        if($this->confirm!==null) {
            $href0 = "javascript:if(confirm('{$this->confirm}')) %s";
        } else {
            $href0 = "javascript:%s";
        }
        switch($this->buttonType)
        {
            case 'submit':
            case 'save':
                $href = sprintf($href0,"jQuery('#{$this->formID}').submit();");
                break;
            case 'reset':
                $href = sprintf($href0,"document.getElementById('{$this->formID}').reset();");
                break;
            case 'cancel':
                $href = sprintf($href0,"self.location.href='".CHtml::normalizeUrl($this->url)."';");
                break;
            default:
                $href = sprintf($href0,"void(0)");
                break;
        }
        $isFirst = '';
        if($this->isFirst)
            $isFirst = ' firstButton';
        $html = <<< EOL
<span class="button {$this->buttonType}$isFirst">
    <a href="{$href}" title="{$this->linkTitle}">
        <span>
            <span>{$this->title}</span>
        </span>
    </a>
</span>
EOL;
        return $html;
    } // }}} 
    // {{{ registerClientScripts
    /**
     * Registers the clientside widget files (css & js)
     */
    private function registerClientScripts() {
        // Get the resources path
        $resources = dirname(__FILE__).'/resources';

        // publish the files
        $baseUrl = Yii::app()->assetManager->publish($resources);

        // register the files

        // register the Stylesheet
        if(is_file($resources.'/button.css')) {
            Yii::app()->clientScript->registerCssFile($baseUrl.'/button.css');
        }
    } // }}}
}

/* vim: set ai sw=4 sts=4 et fdm=marker fdc=4: */ 
